package com.example.dormmanage.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.dormmanage.dao.IHealthDao;
import com.example.dormmanage.dao.IRoomDao;
import com.example.dormmanage.entity.Health;
import com.example.dormmanage.entity.Room;
import com.example.dormmanage.service.IHealthService;
import com.example.dormmanage.view.BillsDto;
import com.example.dormmanage.view.HealthDto;
import com.example.dormmanage.view.ResultView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class HealthService extends ServiceImpl<IHealthDao, Health> implements IHealthService {

    @Autowired
    private IRoomDao roomDao;

    @Autowired
    private BuildingService buildingService;

    public ResultView<Page> selectHealthList(Integer page, Integer pageSize, Integer buildingId, String roomName) {
        Page<HealthDto> pageInfo = new Page<>(page, pageSize);
        //查询宿舍号
        LambdaQueryWrapper<Room> roomLqw = new LambdaQueryWrapper<>();
        roomLqw.eq(Room::getBuildingId, buildingId)
                .like(roomName != null,Room::getRoomName,roomName);
        List<Room> roomList = roomDao.selectList(roomLqw);
        List<HealthDto> healthDtoList = roomList.stream().map(item -> {
            HealthDto healthDto = new HealthDto();
            healthDto.setBuildingName(buildingService.getBuildingName(buildingId));
            healthDto.setRoomName(item.getRoomName());
            LambdaQueryWrapper<Health> healthLqw = new LambdaQueryWrapper<>();
            healthLqw.eq(Health::getRoomId, item.getRoomId());
            Health health = this.getOne(healthLqw);
            BeanUtil.copyProperties(health, healthDto);
            return healthDto;
        }).collect(Collectors.toList());
        pageInfo.setRecords(healthDtoList);
        return ResultView.success(pageInfo);
    }
}
